<template>
    <div class="comment-container">
        <!-- 标题 -->
        <h1>发表评论</h1>
        <hr>
        <!-- 文本框 -->
        <textarea placeholder="请输入要BB的内容（最多吐槽120字）" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size='large' @click="postComment">发表评论</mt-button>
        <!-- 评论列表 -->
        <div class="cmt-list" v-for="(item,i) in comment" :key='i'>
            <div class="cmt-item">
                <!-- 评论标题 -->
                <div class="cmt-title">
                第{{ i+1 }}楼&nbsp;&nbsp;用户：{{ item.user_name }}户&nbsp;&nbsp;发表时间：{{ item.add_time |dateFormat }}
                </div>
                <!-- 评论体 -->
                <div class="cmt-body">{{ item.content }}</div>
            </div>
        </div>
        <!-- 加载更多 -->
        <mt-button type="danger" size='large' plain @click="getMore">加载更多</mt-button>
    </div>
</template>
<script>
import {Toast} from 'mint-ui';
export default {
    data(){
        return {
            msg:'',//发表的内容
            pageIndex:1,//页码下标
            comment:[]//评论集合
        }
    },
    created(){
        this.getComment();
    },
    methods:{
        getComment(){//获取评论列表
            this.$http.get('getcomments/'+this.id).then(result=>{
                console.log(result.body);
                if(result.body.status === 0){
                    this.comment = this.comment.concat(result.body.data);
                }else{
                    Toast('获取评论数据失败');
                }
            })
        },
        getMore(){//加载更多
            this.pageIndex++;
            this.getComment();
        },
        postComment(){//发表评论
            if(this.msg.trim().length === 0){
                return Toast('评论的内容不能为空！');
            }
            console.log(this.msg.trim());
            this.$http.post('postcomment/'+this.id,{
                content:this.msg.trim()
            }).then(result=>{
                console.log(result.body);
                if(result.body.status === 0){
                    var comment = {
                        user_name:"新新匿名用户",
                        add_time:Date.now(),
                        content:this.msg.trim()
                    }  
                    this.comment.unshift(comment);
                    this.msg = '';
                }else{
                    Toast("发表评论失败");
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.comment-container{
    margin-bottom: 10px;
    h1{
        font-size: 18px;
        text-align: center;
        color:#226aff;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
        border-radius: 5px;
    }
    .cmt-list{
        margin:5px 0;
        .cmt-item{
            font-size: 12px;
            .cmt-title{
                line-height: 30px;
                background-color: #ccc;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
    }
}
</style>

